var review = {
	limit: 10,
	course_id: 0,
	page: 1,
	url: {
		list: listReviewUrl,
		add: addReviewUrl,
		delete: deleteReviewUrl
	},
	container_id: '',
	add: function(content) {
		var that = this;
		$.ajax({
			url: this.url.add,
			data: {data: {content: content, course_id: this.course_id}},
			type: 'post',
			dataType: 'json',
			success: function(data) {
				if (data.status) {
					var html = $(data.data.html);
					html.insertAfter(that.container_id + ' .new');
					that.bindDelete(html.find('.jsDeleteReview'));
					$(that.container_id + ' textarea').val('');
					that.gotoBlock();
					$('#askReview' + that.course_id).remove();
				} else {
					if (data.data.message != undefined) {
						that.showError(data.data.message);
					}
				}
			},
			beforeSend: function() {
				that.overlayForm();
			},
			complete: function() {
				that.overlayForm(false);
			},
			statusCode: {
				403: function() {
					window.location = loginUrl;
				}
			}
		})
	},
	delete: function(id) {
		$.ajax({
			url: this.url.delete,
			data: {data: {id: id}},
			type: 'post',
			dataType: 'json',
			success: function(data) {
				if (data.status) {
					var id = '#' + 'review' + data.data.id;
					$(id).remove();
				} else {
					if (data.data.message != undefined) {
						alert(data.data.message);
					}
				}
			},
			statusCode: {
				403: function() {
					window.location = loginUrl;
				}
			}
		});
	},
	bindDelete: function(element) {
		var that = this;
		if (element.length > 0) {
			element.click(function() {
				var id = $(this).data('id');
				that.delete(id);
				return false;
			});
		}
	},
	gotoBlock: function() {
		$('html,body').animate({
			scrollTop: $(this.container_id).offset().top
		});
	},
	bindAdd: function() {
		var that = this;
		$(this.container_id + ' .postSubmit').click(function() {
			var content = $(that.container_id + ' textarea').val();
			if ($.trim(content) == '') {
				that.showError(messages.you_must_enter_the_content_of_review);
			} else {
				that.add(content);
			}
			return false;
		});
	},
	showError: function(message) {
		$(this.container_id + ' .attention').text(message);
	},
	overlayForm: function(show) {
		if (show == undefined || show) {
			$(this.container_id + ' .overlay').show();
		} else {
			$(this.container_id + ' .overlay').hide();
		}
	},
	init: function(courseId, containerId) {
		this.course_id = courseId;
		this.container_id = '#' + containerId;
		this.bindDelete($(this.container_id + ' .jsDeleteReview'));
		this.showError('');
		this.bindAdd();
	}
}

$(function() {
	review.init(courseId, 'reviewList');
});